# -*- encoding: utf-8 -*-
'''
@File    :   real_point.py
@Time    :   2024/07/19
@Author  :   Han Xiao
@Brief   :   描点 (x , y) 暂时只支持新智耘log
'''

import argparse
import re
import matplotlib.pyplot as plt
import random


parser = argparse.ArgumentParser()
parser.add_argument('-l','--log', help="LOG 文件路径，使用绝对路径")
parser.add_argument('-k', "--keys", nargs='+', help="关键字列表")
args = parser.parse_args()


if args.keys:
    key_msgs = args.keys
    if len(key_msgs) == 1:
        key_msgs.append(key_msgs[0])
else:
    parser.print_help()
    exit()

if args.log:
    log_path = args.log
else:
    parser.print_help()
    exit()

patterns = []
for key_msg in key_msgs:
    pattern_key_msg = r'{}\s*\(([^)]+)\)'.format(key_msg)
    patterns.append(re.compile(pattern_key_msg))


with open(log_path, 'r') as f:
    lines = f.readlines()

points_x = [[] for _ in range(len(key_msgs))]
points_y = [[] for _ in range(len(key_msgs))]

for line in lines:
    for i, pattern in enumerate(patterns):
        key_msg_match = pattern.search(line)
        if key_msg_match:
            x, y = key_msg_match.group(1).split(',')
            points_x[i].append(float(x))
            points_y[i].append(float(y))


fig, axs_scatter = plt.subplots(len(key_msgs), 1, figsize=(8, 6 * len(key_msgs)), sharex='col')
colors = [f'#{random.randint(0, 0xFFFFFF):06x}' for _ in range(len(key_msgs))]

for i in range(len(key_msgs)):
    axs_scatter[i].scatter(points_x[i], points_y[i], c=colors[i])
    axs_scatter[i].grid(True)
    axs_scatter[i].legend()

plt.axis('equal')

fig, ax = plt.subplots(figsize=(8, 6))
for i , key_msg in enumerate(key_msgs):
    ax.scatter(points_x[i], points_y[i], label=key_msg, c=colors[i])
    ax.grid(True)

ax.legend()

plt.axis('equal')

plt.show()
